class RolesController < ApplicationController
  before_filter :require_officer

  def index
    @page_description = "Roles"
    @roles = Role.find(:all)
  end

  def create
    @role = Role.new(params[:role])
    if @role.save
      flash[:notice] = "#{@role.name} created."
    end
    redirect_to :action => :index
  end

  def edit
    @page_description = "Edit role"
    @roles = Role.find(:all, :order => "name")
  end

  def update
    role = Role.find(params[:id])
    if role.update_attributes(params[:role])
      flash[:notice] = "#{role.name} updated."
    else
      flash[:notice] = "Failed to update #{role.name}."
    end

    redirect_to :action => :index
  end

  def delete
    if request.get?
      flash[:notice] = "Select the roles you want to delete."
    else
      role_was_deleted = false
      params.each_key { |k|
        if (k =~ /^deleterole([[:digit:]]+)$/)
          Role.destroy($1.to_i)
          role_was_deleted = true
        end
      }
      if role_was_deleted
        flash[:notice] = "Roles deleted."
      else
        flash[:notice] = "Select roles to delete."
      end
    end
    redirect_to :action => :index
  end

end
